using UnityEngine;

public class AnimationService : FSMServiceiBase
{
    public float normalizedTime;
    public string now_play_id;
    public override void OnAnimationEnd(PlayerState state)
    {
        base.OnAnimationEnd(state);
    }

    public void Play(PlayerState state)
    {
        normalizedTime = 0;
        now_play_id = state.excel_Config.anm_name;
        player.animator.Play(state.excel_Config.anm_name,0,0);
        player.animator.Update(0);
    }
    public override void OnBegin(PlayerState state)
    {
        base.OnBegin(state);
        Play(state);
    }

    public override void OnDisable(PlayerState state)
    {
        base.OnDisable(state);
    }

    public override void OnEnd(PlayerState state)
    {
        base.OnEnd(state);
    }

    bool call_end;
    public override void OnUpdate(float normalizedTime, PlayerState state)
    {
        base.OnUpdate(normalizedTime, state);

        if (!string.IsNullOrEmpty(now_play_id))
        {
            AnimatorStateInfo info = player.animator.GetCurrentAnimatorStateInfo(0);

            //0-1 表示动作 0-100的进度
            if (info.IsName(now_play_id))
            {
                this.normalizedTime = info.normalizedTime;
                if(normalizedTime > 1)
                {
                    if(call_end ==false)
                    {
                        this.normalizedTime = 1;
                        player.AnimationOnPlayEnd(); //结束调用一下
                        call_end = true;
                    }
                    else
                    {
                        call_end =false;
                        this.normalizedTime = normalizedTime%1;
                    }
                }
            }
            else 
                this.normalizedTime = 0;
        }

    }

    public override void ReLoop(PlayerState state)
    {
        base.ReLoop(state);
    }

    public override void ReStart(PlayerState state)
    {
        base.ReStart(state);
    }
}